//
//  CZComposeToolBar.swift
//  WHWeiBo01
//
//  Created by whitcast on 16/7/18.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

//  发微博toolbar按钮的枚举类型
enum CZComposeToolBarButtonType: Int {
    //  图片
    case Picture = 0
    //  @
    case Mention = 1
    //  话题
    case Trend = 2
    //  表情
    case Emoticon = 3
    //  加号
    case Add = 4
}

//  发微博toolbar
/*
    自身不具备渲染效果, 依赖子控件的显示
*/
class CZComposeToolBar: UIStackView {
    //  定义点击toolbar按钮的闭包
    var selectedToolBarButtonClosure: ((type: CZComposeToolBarButtonType)->())?
    //  表情按钮
    var emotionButton: UIButton?
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
          //自身不具备渲染效果, 依赖子控件的显示
//        backgroundColor = UIColor.yellowColor()
        
        //  设置布局方式
        axis = .Horizontal
        //  子控件填充方式
        distribution = .FillEqually
        
        addChildButton("compose_toolbar_picture", type: .Picture)
        addChildButton("compose_mentionbutton_background", type: .Mention)
        addChildButton("compose_trendbutton_background", type: .Trend)
        emotionButton = addChildButton("compose_emoticonbutton_background", type: .Emoticon)
        addChildButton("compose_add_background", type: .Add)
    
    }
    
    
    //  添加子控件按钮
    private func addChildButton(imageName: String, type: CZComposeToolBarButtonType) -> UIButton {
        let button = UIButton()
        //  获取枚举的原始值
        button.tag = type.rawValue
        //  添加点击事件
        button.addTarget(self, action: "buttonAction:", forControlEvents: .TouchUpInside)
        
        //  设置图片
        button.setImage(UIImage(named: imageName), forState: .Normal)
        button.setImage(UIImage(named: "\(imageName)_highlighted"), forState: .Highlighted)
        //  设置背景图片
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: .Normal)
        button.adjustsImageWhenHighlighted = false
        
        //  添加控件
        addArrangedSubview(button)
        
        return button
    }
    
    
    // MARK: --点击事件
    @objc private func buttonAction(button: UIButton) {
        //  通过枚举原始值获取枚举成员
        let type = CZComposeToolBarButtonType(rawValue: button.tag)!
        
    
        //  执行闭包回调
        selectedToolBarButtonClosure?(type: type)
        
    }
    
    //  根据外界键盘类型设置不同的图标
    func showEmotionIcon(isEmoticon: Bool) {
        //  如果是表情键盘类型,那么显示键盘icon
        //  如果是系统键盘类型,那么显示表情icon
        
        if isEmoticon {
            //  显示键盘icon
            emotionButton?.setImage(UIImage(named: "compose_keyboardbutton_background"), forState: .Normal)
            emotionButton?.setImage(UIImage(named: "compose_keyboardbutton_background_highlighted"), forState: .Highlighted)
        } else {
            emotionButton?.setImage(UIImage(named: "compose_emoticonbutton_background"), forState: .Normal)
            emotionButton?.setImage(UIImage(named: "compose_emoticonbutton_background_highlighted"), forState: .Highlighted)
        }
        
        
        
        
        
        
        
        
        
    }
    
    
    
    
    
    
    
    

}
